
const Controller = require('egg').Controller
/**
 * @controller 用户 User
 */
class UserController extends Controller {
  /**
   * @summary 获取个人信息
   * @router get /myInfo
   * @response 200 response<myInfoRes> OK
   * @Authorization
   */
  async myInfo() {
    const { ctx } = this

    const res = await ctx.service.user.myInfo()
    if (res) {
      ctx.renderBack(res)
    }
  }
  /**
   * @summary 个人信息修改
   * @router put /myInfo
   * @request body modifyInfoReq
   * @response 200 response<normalRes> OK
   * @Authorization
   */
  async modifyInfo() {
    const { ctx } = this
    const validateResult = await ctx.validate('user.modifyInfo', ctx.request.body)
    if (!validateResult) return

    const res = await ctx.service.user.modifyInfo(ctx.request.body)
    if (res && res[0] === 1) {
      ctx.renderBack()
    }
  }
  // 获取用户列表
  async getUserList() {
    const { ctx } = this
    const res = await ctx.service.user.getUserList(ctx.query)
    if (res) {
      return ctx.renderBack(res)
    }
  }
  // 删除用户
  async deleteUser() {
    const { ctx } = this
    const res = await ctx.service.user.deleteUser(ctx.query.id)
    if (res) {
      return ctx.renderBack(null, '1', '删除用户成功')
    }
    return ctx.renderBack(null, '0', '删除用户失败')
  }
}

module.exports = UserController
